package top.resty.erp.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.resty.erp.domain.Products;
import top.resty.erp.domain.Storage;
import top.resty.erp.service.BrandService;
import top.resty.erp.service.ProductService;
import top.resty.erp.service.StorageService;
import top.resty.erp.util.ErpUtils;
import top.resty.erp.util.LayuiJson;

import java.util.List;

/**
 * <pre>
 *
 * </pre>
 *
 * @author : resty
 * @date : 2021年03月15 17:45
 */
@Controller
@RequestMapping("/products")
public class ProductsController {
	
	@Autowired
	private BrandService brandService;
	
	@Autowired
	private ProductService productService;
	
	@RequestMapping("/findProductsForVariety")
	@ResponseBody
	public LayuiJson<Products> findProductsForVariety(String variety,int page,int limit){
		Page<Object> pages = PageHelper.startPage(page, limit);
		LayuiJson<Products> li = new LayuiJson<>();
		List<Products> varieties = productService.findProductsForVariety(variety);
		li.setCount((int) pages.getTotal());
		li.setData(varieties);
		return li;
	}
	
	@RequestMapping("/findProductsForSupplier")
	@ResponseBody
	public LayuiJson<Products> findProductsForSupplier(String supplier,int page,int limit){
		Page<Object> pages = PageHelper.startPage(page, limit);
		LayuiJson li = new LayuiJson();
		List<Products> list = productService.findProductsForSupplier(supplier);
		li.setCount((int) pages.getTotal());
		li.setData(list);
		return li;
	}
	
	@RequestMapping("/warehouseIdAllList")
	@ResponseBody
	public List<Products> warehouseIdAllList(String warehouseId){
		return productService.warehouseIdAllList(warehouseId);
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String deletePruduct(Integer id ){
		return productService.deleteProducts(id)?"success":"fail";
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String updateProducts(String keyStr,String valueStr,Integer id){
		return productService.updateProducts(keyStr, valueStr, id)?"成功":"失败";
	}
	
	
	@RequestMapping("/warehouseIdList")
	@ResponseBody
	public LayuiJson<Products> warehouseIdList(String warehouseId, int page,int limit){
		
		Page<Object> pageObj = PageHelper.startPage(page, limit);
		LayuiJson layuiJson = new LayuiJson();
		
		List<Products> products = productService.warehouseIdList(warehouseId);
		
		layuiJson.setCount((int) pageObj.getTotal());
		layuiJson.setData(products);
		
		
		return  layuiJson;
	}
	
	
	
	@RequestMapping("/likeWarehouseId")
	@ResponseBody
	public List<String> findLikeWarehouseId(String warehouseId){
		return productService.findByLikeWarehouseId(warehouseId);
	}
	
	
	@RequestMapping("/findTelephonesForContact")
	@ResponseBody
	public List<String> findTelephonesForContact(String contact){
		return productService.findTelephonesForContact(contact);
	}
	
	@RequestMapping("/findContactForSupplier")
	@ResponseBody
	public List<String> findContactForSupplier(String supplier){
		return productService.findAllContactsForSupplier(supplier);
	}
	
	@Autowired
	private StorageService storageService;
	
	@RequestMapping("/addList")
	@ResponseBody
	public String addProductsList(@RequestBody String products){
		
		int start = products.lastIndexOf(":")+2;
		int end = products.lastIndexOf("\"");
		String remark = products.substring(start, end);
		
		List<Products> list = JSONObject.parseArray(products, Products.class);
		list.forEach((item)->{
			item.setRemake(remark);
			Storage storageByProducts = storageService.findStorageByProducts(item);
			if(storageByProducts==null){
				System.out.println("添加库存");
				Storage storage = new Storage();
				storage.setStandard(item.getStandard());
				storage.setType(item.getTypeName());
				storage.setVariety(item.getVarietyName());
				storage.setCount(item.getCount());
				storageService.saveStorage(storage);
			}else{
				System.out.println("修改库存");
				//通过products 获取 storage的id
				int count = storageByProducts.getCount()+item.getCount();
				storageService.updateCount(count,storageByProducts.getId());
			}
		});
		
		return String.valueOf(productService.saveProducts(list));
	}
	
	
	@RequestMapping("/add")
	public String goToProductsAddPage(Model model){
		model.addAttribute("brands",brandService.getAllBrands());
		model.addAttribute("warehouseId", ErpUtils.getWareHouseId());
		//将所有的供应商搜索并添加  supplier
		model.addAttribute("suppliers", productService.findAllSuppliers());
		return "man/product_add";
	}
	
	@RequestMapping("/list")
	public String goToProductsChangePage(Model model){
		//查询所有的产品名称
		model.addAttribute("varieties", productService.findAllVarieties());
		//查询所有的供应商
		model.addAttribute("suppliers", productService.findAllSuppliers());
		return "man/product_list";
	}
	
	
	
	
}
